/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Desarrollo.Soft.Hibernate.ViewPrincipal.views;

import Desarrollo.Soft.Hibernate.Control.ctrlGeneric;
import Desarrollo.Soft.Hibernate.Data.Entity.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author r0ck3r
 */
public class intFrmProductos extends javax.swing.JInternalFrame {

    
    
    /**
     * Creates new form intFrmProductos
     */
    private boolean productoProveedor = false;
    private List<Catdepartamento> listaDepartamentos;
    private DefaultTableModel modeloTabla = new DefaultTableModel();
    private List<Catproductos> lista ;
    private intFrmProductosProveedores frmProductoProveedor = null;
    
    public intFrmProductos() {
        initComponents();
        cargarCombo();
        encavezadosTabla();
        cargarProductos();
        
    }

    public intFrmProductos(intFrmProductosProveedores _frmProducProveedor,boolean _proveedoresProductos) {
        initComponents();
        encavezadosTabla();
       this.frmProductoProveedor = _frmProducProveedor;
       this.productoProveedor = _proveedoresProductos;
       
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtnombreProducto = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtDescripcion = new javax.swing.JTextArea();
        jToolBar1 = new javax.swing.JToolBar();
        jLabel3 = new javax.swing.JLabel();
        btnGuardar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        cmbDepartamentos = new javax.swing.JComboBox();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblProductos = new javax.swing.JTable();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Productos"));

        jLabel1.setText("Nombre Producto: ");

        jLabel2.setText("Descripcion: ");

        txtDescripcion.setColumns(20);
        txtDescripcion.setRows(5);
        jScrollPane1.setViewportView(txtDescripcion);

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);

        jLabel3.setText("                                                                          ");
        jToolBar1.add(jLabel3);

        btnGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Desarrollo/Soft/Hibernate/ViewPrincipal/Imagenes/Save.gif"))); // NOI18N
        btnGuardar.setText("Guardar");
        btnGuardar.setFocusable(false);
        btnGuardar.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btnGuardar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Desarrollo/Soft/Hibernate/ViewPrincipal/Imagenes/Delete.gif"))); // NOI18N
        btnCancelar.setText("Cancelar");
        btnCancelar.setFocusable(false);
        btnCancelar.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btnCancelar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        jLabel4.setText("Departamento: ");

        tblProductos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(tblProductos);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(55, 55, 55)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(19, 19, 19)
                                .addComponent(txtnombreProducto, javax.swing.GroupLayout.PREFERRED_SIZE, 224, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(19, 19, 19)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 253, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addGap(33, 33, 33)
                                .addComponent(cmbDepartamentos, javax.swing.GroupLayout.PREFERRED_SIZE, 224, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(146, 146, 146)
                        .addComponent(btnGuardar)
                        .addGap(18, 18, 18)
                        .addComponent(btnCancelar)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtnombreProducto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(107, 107, 107)
                        .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(36, 36, 36))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(cmbDepartamentos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(24, 24, 24)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnCancelar)
                    .addComponent(btnGuardar))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(308, 308, 308))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 534, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 393, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void encavezadosTabla()
    {
        try {
            String[] encabezado = {"Nombre", "Descripcion", "Departamento"};
            modeloTabla.setColumnIdentifiers(encabezado);
            tblProductos.setModel(modeloTabla);
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }
    
    public void cargarProductos()
    {
        try {
            
             for (int i = tblProductos.getRowCount()-1; i >= 0; i--) {
                 modeloTabla.removeRow(i);
            }
             lista =  ctrlGeneric.selectItems(Catproductos.class);
            for (int i = 0; i < lista.size(); i++) {
                String[] array = new String[3];
                array[0] = lista.get(i).getValor();
                array[1] = lista.get(i).getDescripcion();
                array[2] = lista.get(i).getCatdepartamento().getValor().toString();
                               
                modeloTabla.addRow(array);
            }
            tblProductos.setModel(modeloTabla);
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }
    
   
    
    public void cargarCombo()
    {
        try {
            listaDepartamentos = ctrlGeneric.<Catdepartamento>selectItems(Catdepartamento.class);
            cmbDepartamentos.removeAllItems();
            cmbDepartamentos.addItem("");
            for (int i = 0; i < listaDepartamentos.size(); i++) {
                cmbDepartamentos.addItem(listaDepartamentos.get(i).getValor());
            }
        } catch (Exception e) {
            System.out.print(e.getMessage());
        }
    }
    
    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
       
        try {
                if(txtnombreProducto.getText().equals("") && txtDescripcion.getText().equals(""))
                {
                    JOptionPane.showMessageDialog(null, "Todos los campos son obligatorios");
                }else{
                    Catproductos producto = new Catproductos();
                    producto.setValor(txtnombreProducto.getText());
                    producto.setDescripcion(txtDescripcion.getText());
                    producto.setCatdepartamento(listaDepartamentos.get(cmbDepartamentos.getSelectedIndex()-1));

                    boolean respuest = ctrlGeneric.<Catproductos>createItem(producto);
                    if(productoProveedor)
                    {
                        if(respuest)
                        {
                            this.frmProductoProveedor.cargarCombo();
                            this.frmProductoProveedor.seleccionarProductoAgragado(producto);
                            this.dispose();
                        }else{
                            JOptionPane.showMessageDialog(null, "Ocurrio un Error Inesperado!!");
                        }
                    }else
                    {
                        if(respuest)
                        {
                            cargarProductos();
                            JOptionPane.showMessageDialog(null, "Producto guardado Correctamente!!");
                            
                            
                        }else{
                            JOptionPane.showMessageDialog(null, "Ocurrio un Error Inesperado!!");
                        }
                    }
                }

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        
       
    }//GEN-LAST:event_btnGuardarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        try {
             if(!txtnombreProducto.getText().equals("") || !txtDescripcion.getText().equals(""))
             {
                 if(JOptionPane.showConfirmDialog(null, "Seguro que desea Cancelar ?")==0 )
                 {
                     this.dispose();
                 }
             }
             else
             {
                 this.dispose();
             }
        } catch (Exception e) {
        }
    }//GEN-LAST:event_btnCancelarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JComboBox cmbDepartamentos;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JTable tblProductos;
    private javax.swing.JTextArea txtDescripcion;
    private javax.swing.JTextField txtnombreProducto;
    // End of variables declaration//GEN-END:variables
}
